package com.syn.vr.repository;


import com.gaoyi.basemodule.baseCallback.Callback;
import com.gaoyi.basemodule.baseCallback.OnResultListener;
import com.gaoyi.basemodule.baseUtil.OKHttpUtil;
import com.gaoyi.basemodule.baseUtil.ThreadManager;
import com.syn.vr.db.dao.RecordDao;
import com.syn.vr.db.entity.Record;

import java.lang.reflect.Type;
import java.util.List;

public class TagRepository extends BaseRepository<Record, RecordDao> {

    @Override
    protected RecordDao getDao() {
        return mDatabase.getRecordDao();
    }

    /**
     * 单条上传至服务器
     */
    public void add(Object bean, OnResultListener onResultListener, Type typeofT) {
        OKHttpUtil.getInstance().postAsync(OKHttpUtil.NET_URL + "/api/VRST/OnlineServices/AddCourseRecord", bean, new Callback() {
            @Override
            public void onSuccess(Object obj) {
                onResultListener.onSuccess(obj);
            }

            @Override
            public void onFailed(Exception e) {
                onResultListener.onFails(e);
            }
        }, typeofT);
    }

    /**
     * 多条上传至服务器
     */
    public void adds(Object bean, OnResultListener onResultListener, Type typeofT) {
        OKHttpUtil.getInstance().postSyncThread(OKHttpUtil.NET_URL + "/api/VRST/OnlineServices/AddCourseRecord", bean, new Callback() {
            @Override
            public void onSuccess(Object obj) {
                onResultListener.onSuccess(obj);
            }

            @Override
            public void onFailed(Exception e) {
                onResultListener.onFails(e);
            }
        }, typeofT);
    }

    public void getCourses(final OnResultListener onResultListener, Type typeofT) {
        OKHttpUtil.getInstance().getAsync(OKHttpUtil.NET_URL + "/api/VRST/OnlineServices/GetCourse",
                new Callback() {
                    @Override
                    public void onSuccess(Object obj) {
                        onResultListener.onSuccess(obj);
                    }

                    @Override
                    public void onFailed(Exception e) {
                        onResultListener.onFails(e);
                    }
                }, typeofT);
    }

    public void getWorker(String cardNo, final OnResultListener onResultListener, Type typeofT) {
        OKHttpUtil.getInstance().getAsync(OKHttpUtil.NET_URL + "/api/VRST/OnlineServices/GetWorkInfo?card=" + cardNo,
                new Callback() {
                    @Override
                    public void onSuccess(Object obj) {
                        onResultListener.onSuccess(obj);
                    }

                    @Override
                    public void onFailed(Exception e) {
                        onResultListener.onFails(e);
                    }
                }, typeofT);
    }

    public void insert(Record record) {
        ThreadManager.getInstance().execute(() -> mDao.insert(record));
    }

    /**
     * 查询前limit条未上传的记录
     */
    public List<Record> queryUnUpload() {
        return mDao.query(0, 1);
    }

    public void update(List<Record> list) {
        mDao.update(list);
    }

    public void update(Record record) {
        mDao.update(record);
    }

}
